/**
 * 作者: 会写BUG
 * 创作日期: 2025/3/25 21:23
 * 描述: cwbQtPractice.DisplayPanel
 *
 *
 **/

#pragma once

#include "display_panel.h"
#include "score_panel.h"
#include "menu_panel.h"
#include "score_record.h"

#include <QPainter>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <QPropertyanimation>

DisplayPanel::DisplayPanel(QWidget * _parent)
    : QWidget(_parent) {
    initUI();
    initConnect();
}

DisplayPanel::~DisplayPanel() = default;

MenuPanel* DisplayPanel::menuPanel() const
{
    return menu_panel_;
}

void DisplayPanel::initUI() {
    score_panel_ = new ScorePanel();
    menu_panel_ = new MenuPanel();

    stacked_widget_ = new QStackedWidget(this);
    QVBoxLayout * main_layout = new QVBoxLayout(this);
    main_layout->setSpacing(0);
    main_layout->setContentsMargins(0, 0, 0, 0);
    main_layout->addWidget(stacked_widget_, 1);
    stacked_widget_->addWidget(score_panel_);
    stacked_widget_->addWidget(menu_panel_);

}

void DisplayPanel::initConnect()
{
    connect(score_panel_, &ScorePanel::menuClicked,
        this, &DisplayPanel::menuClicked);

    connect(score_panel_, &ScorePanel::menuClicked,
            this, &DisplayPanel::menuClicked);
}

void DisplayPanel::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    painter.setPen(Qt::transparent);
    painter.setBrush(QColor(173, 133, 81));

    painter.drawRect(0, 0, width(), height());
}

void DisplayPanel::menuClicked()
{
    QPropertyAnimation * animation = new QPropertyAnimation(menu_panel_, "geometry");
    animation->setDuration(1000);
    animation->setStartValue(QRect(0, height(),
        width(), height()));
    animation->setEndValue(QRect(0, 0, width(), height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    stacked_widget_->setCurrentIndex(1);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}
